// pages/advise/advise.js
var util = require('../../utils/util.js')
var app = getApp()
var index = 0
Page({
  /**
   * 页面的初始数据
   */
  data: {
    textareaValue: '', //文本框输入的值
    imgUrls: [], //上传图片地址
    imgnotice: false, //大图==是否隐藏点击添加照片
    showAddImg: true, //小图==是否隐藏点击添加照片
    paramsImgUrls: [], //从后台获取的图片地址列表-提交投诉建议
    isAllUp: 0,
    complainText:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  // 文本框输入值
  textareaInput: function(e) {
    var that = this
    that.setData({
      textareaValue: e.detail.value
    })
  },
  // 选择图片
  chooseImage: function() {
    var that = this
    var imgCount = 3
    var slectImgNum = that.data.imgUrls.length
    if (slectImgNum < 3) {
      wx.chooseImage({
        count: imgCount - slectImgNum,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function(res) {
          console.log(res.tempFilePaths)
          that.setData({
            imgUrls: that.data.imgUrls.concat(res.tempFilePaths),
            imgnotice: true,
            showAddImg: false
          })
          // 选择完三张后面添加icon隐藏
          console.log('选择图片的临时地址' + that.data.imgUrls)
          console.log('===已选择图片张数====' + that.data.imgUrls.length)
          if (that.data.imgUrls.length == 3) {
            that.setData({
              showAddImg: true
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '最多选择选择3张图片',
      })
    }

  },
  // 图片删除
  delImg: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    console.log('===当前要删除图片index===' + index)
    that.data.imgUrls.splice(index, 1) //删除数据中当前选择下标为index的元素，返回新数组
    // 全部删除显示添加图片大图标
    if (that.data.imgUrls.length == 0) {
      that.setData({
        imgUrls: [],
        showAddImg: true,
        imgnotice: false
      })
    } else {
      that.setData({
        imgUrls: that.data.imgUrls, //将新数组传递给页面
        showAddImg: false
      })
    }
  },

  // 图片上传函数  
  upImg: function() {
    var that = this
    console.log(index + that.data.imgUrls[index])
    let token = getApp().globalData.mohuanUser["X-AUTH-TOKEN"]
    wx.uploadFile({
      url: 'https://www.magicmanufactory.com/c2mwebservice/api/v2/test/WXSmartProgram',
      filePath: that.data.imgUrls[index],
      name: 'picFile',
      async: false,
      formData: {
        id: 'complain'
      },
      header: {
        'Accept': 'application/json',
        'X-AUTH-TOKEN': token
      },
      success: function(res) {},
      complete: function(res) {
        index++
        // console.log('图片index' + index)        
        var imgUrlArry = JSON.parse(res.data)
        console.log('返回的地址====' + imgUrlArry.returnData[0])

        var newimgUrlArryitem = imgUrlArry.returnData[0]

        that.data.paramsImgUrls.push(newimgUrlArryitem.toString())
        console.log('提交投诉建议图片地址数组' + that.data.paramsImgUrls)
        console.log('22222')
        //  若图片未上传完成继续调用此函数
        if (index < that.data.imgUrls.length) {
          that.upImg()
        } else {
          index = 0
          // wx.showToast({
          //   title: '图片上传完成',
          //   icon:'success'
          // })
          that.setData({
            imgUrls: [],
            showAddImg: true,
            imgnotice: false,
            isAllUp: 1 //全部上传完成         
          })
          console.log('==图片是否上传完成===' + that.data.isAllUp)

          // second step - 提交投诉建议接口

          var member = app.globalData.mohuanUser
          var memberId = member.memberId
          var token = member["X-AUTH-TOKEN"]
          if (that.data.isAllUp == 1) { //图片全部上传成功之后调用接口
            console.log('参数-' + that.data.textareaValue)
            console.log('参数二' + that.data.paramsImgUrls)
            var params = {
              content: that.data.textareaValue,
              picUrls: that.data.paramsImgUrls
            }
            console.log('--------6666-------' + params.picUrls)
            wx.request({
              url: 'https://www.magicmanufactory.com/c2mwebservice/api/v2/complain/' + memberId + '/add',
              data: params,
              method: 'post',
              header: {
                'content-type': 'application/json', // 默认值
                'X-AUTH-TOKEN': token,
              },
              success: function(res) {
                console.log(res)
                if (res.data.returnCode == 200) {
                  wx.showToast({
                    title: '提交成功',
                    icon: 'success'
                  })
                  that.setData({
                    complainText: ''
                  })
                } else {
                  wx.showToast({
                    title: res.data.returnMsg,
                    image: '../../images/chahao.png'
                  })
                }


              }
            })

          }


        }
      }
    })

  },

  // 投诉建议提交
  upAdvise: function() {
    var that = this
    //  提交接口需要的参数
    var member = app.globalData.mohuanUser
    var memberId = member.memberId
    var token = member["X-AUTH-TOKEN"]
    // 条件必填
    if (that.data.textareaValue == '' && that.data.textareaValue != undefined) {
      wx.showToast({
        title: '请填写投诉建议',
        image: '../../images/chahao.png'
      })
      return false;
    }
    // 只有文本内容没有图片
    console.log('===是否有上传图片==' + that.data.imgUrls.length)
    console.log(memberId)
    if (that.data.imgUrls.length > 0) {
      that.upImg()
    } else {
      // 没有图片只有文本
      var params = {
        content: that.data.textareaValue,
        picUrls: []
      }
      wx.request({
        url: 'https://www.magicmanufactory.com/c2mwebservice/api/v2/complain/' + memberId + '/add',
        data: params,
        method: 'post',
        header: {
          'content-type': 'application/json', // 默认值
          'X-AUTH-TOKEN': token,
        },
        success: function (res) {
          console.log(res)
          if (res.data.returnCode == 200) {
            wx.showToast({
              title: '提交成功',
              icon: 'success'
            })
            that.setData({
              complainText: ''
            })
          } else {
            wx.showToast({
              title: res.data.returnMsg,
              image: '../../images/chahao.png'
            })
          }
        }
      })
    }





  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})